const fetch = require("../../module/service.js");
const get_from = require("../../module/service.js").get_from;
const post_to = require("../../module/service.js").post_to;
Page({
  data: {
    is_login: false,
    user_name: '',
    user_pwd: '',
    image_url:'../../images/she.jpg',
    tmp_addr: '',
    my_addr: '我的宿舍',
    tmp_pwd: '',
    modify_pwd: false,
    modify_addr: false,
    recoredarray:[],
    is_shop_owner: false,
    shop_id: 0
  },
  onLoad: function (options) {
    this.is_login = getApp().globalData.userInfo.is_login;
  },
  getNameInput(e){
    let input = e.detail.value;
    // console.log(input);
    if (input)
    {
      this.data.user_name = input;
    }
  },

  getPwdInput(e){
    let input = e.detail.value;
    // console.log(input);
    if (input)
    {
      this.data.user_pwd = input;
    }
  },

  modifyAddr_text(e){
    let input = e.detail.value;
    // console.log(input);
    if (input)
    {
      this.data.tmp_addr = input;
    }
  },

  //登录
  login(){
    let userInfo = {
      name: this.data.user_name,
      pwd: this.data.user_pwd,
    };

    wx.showLoading({
      title: '登陆中...',
      mask: true
    });

    post_to('user/login', userInfo).then((res)=>{
      if (res.data.msg !== 'success')
      {
        wx.showToast({
          title: '信息错误!',
          icon: 'error',
          duration: 1000
        });
        wx.hideLoading();
        console.log(res.data.msg);
        return;
      }
      getApp().globalData.userInfo.user_id = res.data.user_id;
      getApp().globalData.userInfo.is_login = true;
      //设置数据
      this.setData({
        is_login: true,
        my_addr: res.data.addr,
        modify_addr: false,
        tmp_pwd: this.data.user_pwd,
        is_shop_owner: res.data.is_shop_owner ? true : false,
        shop_id: res.data.shop_id,
        user_name: this.data.user_name
      });
      console.log(this.data.is_shop_owner);
      // console.log(this.data.user_pwd);
      wx.showToast({
        title: '登录成功!',
        icon: 'success',
        duration: 1000
      });
      wx.hideLoading();
    }, (err)=>{
      wx.showToast({
        title: '登陆失败',
        icon: 'error',
        duration: 1000
      });
      wx.hideLoading();
      console.log(err);
    });
  },

  //退出登录
  logout(){
    getApp().globalData.userInfo.user_id = 0;
    getApp().globalData.userInfo.is_login = false;
    this.setData({
      is_login: false,
      user_name: '',
      user_pwd: '',
      tmp_addr: '',
      my_addr: '我的宿舍',
      tmp_pwd: '',
      modify_pwd: false,
      modify_addr: false,
      recoredarray:[],
      is_shop_owner: false
    });
    wx.showToast({
      title: '登出成功!',
      icon: 'success',
      duration: 1000
    });
  },

  //修改地址
  modifyAddr(){
    this.setData({
      modify_addr: true,
      tmp_addr: this.data.my_addr
    });
  },

  //确认修改地址
  modifyAddr_ensure(){
    if (this.data.my_addr == this.data.tmp_addr)
    {
      this.setData({
        modify_addr: false,
        tmp_addr: ''
      });
      console.log('地址值相同，无需修改');
      console.log(this.data.my_addr);
      return;
    }

    let post_param = {
      user_id: getApp().globalData.userInfo.user_id,
      new_addr: this.data.tmp_addr 
    };
    post_to('user/modify_addr', post_param).then((res)=>{
      let msg = res.data.msg;
      if (msg !== "success")
      {
        wx.showToast({
          title: '失败！',
          icon: 'error'
        });
        return;
      }

      this.setData({
        modify_addr: false,
        my_addr: this.data.tmp_addr
      });

    });
  },

  //取消修改地址
  modifyAddr_cancle(){
    this.setData({
      modify_addr: false,
      tmp_addr: ''
    });
  },

  //用户注册
  register(){
    let userInfo = {
      name: this.data.user_name,
      pwd: this.data.user_pwd,
      is_shop_owner: 0    //在小程序端注册的默认不是食堂管理者
    };
    
    // wx.showModal({
    //   title: '请确认',
    //   content: '您是否是食堂管理者',
    //   confirmText: '是',
    //   cancelText: '否',
    //   complete: (res) => {
    //     if (res.cancel) {
    //       userInfo.is_shop_owner = 0
    //     }
    //     else if (res.confirm) {
    //       userInfo.is_shop_owner = 1
    //     }

        
    //   }
    // });

    wx.showLoading({
      title: '注册中...',
      mask: true,
    });

    post_to('user/register', userInfo).then((res)=>{
      if (res.data.msg !== 'success')
      {
        if (res.data.msg == 'username has been used')
        {
          wx.showToast({
            title: '用户名被占用!',
            icon: 'error',
            duration: 1000
          });
        }
        wx.hideLoading();
        console.log(res.data.msg);
        return;
      }
      getApp().globalData.userInfo.user_id = res.data.user_id;
      getApp().globalData.userInfo.is_login = true;
      //设置数据
      this.setData({
        is_login: true,
        my_addr: res.data.addr,
        modify_addr: false
      });
      // console.log(this.data.is_login);
      wx.showToast({
        title: '注册成功!',
        icon: 'success',
        duration: 1000
      });
      wx.hideLoading();
    }, (err)=>{
      wx.showToast({
        title: '注册失败',
        icon: 'error',
        duration: 1000
      });
      wx.hideLoading();
      console.log(err);
    });

  },

  //忘记密码
  modifyPwd_text(e){
    let input = e.detail.value;
    // console.log(input);
    if (input)
    {
      this.data.tmp_pwd = input;
      // console.log(this.data.tmp_pwd);
    }
  },

  modifyPwd_ensure(){
    if (this.data.user_pwd == this.data.tmp_pwd)
    {
      this.setData({
        modify_pwd: false,
      });
      console.log('密码值相同，无需修改');
      console.log(this.data.tmp_pwd);
      return;
    }

    let userInfo = {
      user_id: getApp().globalData.userInfo.user_id,
      pwd: this.data.tmp_pwd,
    };
    post_to('user/resetPwd', userInfo).then((res)=>{
      let msg = res.data.msg;
      if (msg !== "success")
      {
        wx.showToast({
          title: '失败！',
          icon: 'error'
        });
        return;
      }

      this.setData({
        modify_pwd: false,
        user_pwd: this.data.tmp_pwd,
        tmp_pwd: this.data.tmp_pwd
      });
    }, (err)=>{
      wx.showToast({
        title: '修改失败',
        icon: 'error',
        duration: 1000
      });
      console.log(err);
    });
  },

  //修改密码
  modifyPwd(){
    this.setData({
      modify_pwd: true,
      tmp_pwd: this.data.user_pwd
    });
    console.log(this.data.user_pwd);
  },

  //取消修改密码
  modifyPwd_cancle(){
    this.setData({
      modify_pwd: false,
    });
  },

  // 实现商店用户管理商店方法
  to_shop_mann(){
    if (!this.data.shop_id)
    {
      wx.showToast({
        title: '非商店管理员！',
      })
      return;
    }
    //跳转到商店管理页面
    wx.navigateTo({
      url: '/pages/shop_mann/shop_mann?shop_id='+this.data.shop_id,
    }, ()=>{
      wx.showToast({
        title: '跳转失败',
      })
    });
  },

  initiatePayment(){
    const that = this;
    wx.request({
      url: 'http://127.0.0.1:3000/api/getPayParams',
      method: 'POST',
      data: {
        orderId: 123,
      },
      success: function (res) {
        const payParams = res.data;
        that.wxPay(payParams);
      },
      fail: function (error) {
        console.error('Failed to get payment parameters:', error);
      }
    });
  },

  wxPay(payParams)
    {
      wx.requestPayment({
        timeStamp: payParams.timeStamp,
        nonceStr: payParams.nonceStr,
        package: payParams.package,
        signType: payParams.signType,
        paySign: payParams.paySign,
        success: function (res) {
          console.log('Payment success:', res);
          // 处理支付成功的逻辑
        },
        fail: function (error) {
          console.error('Payment failed:', error);
          // 处理支付失败的逻辑
        }
      });
    }
})